/**
 * 
 * create at 9-2-17
 */

const _ = require('lodash');

registerController.$inject = ['$scope','$location','registerService'];
function registerController($scope,$location,registerService){
    var vm = this;
    var s = sessionStorage.getItem("user");
    // console.log(JSON.parse(s).username);
    vm.register = function(){
        var user = {
            username:vm.username,
            password:vm.password
        }
        var myForm = document.querySelector('#myForm');
        var formData = new FormData(myForm);
        for (var pair of formData.entries()) {
              console.log(pair[0] + ', ' + pair[1]);
            }
        console.log('=====>into');
        if(_.isEqual(user.password,vm.password2)){
            if(vm.username !=null && vm.password != null){
                registerService.register(formData,function(err,result){
                    if(err) console.log('register error');
                    else{
                        if(result.flag == 1){
                            vm.massage = '用户名已经存在！'
                        }else{
                            $location.path('/login');
                        }
                    }
                });
            }
        }else{
            vm.massage = '两次输入的密码必须相同！';
        }
    }
}

module.exports = registerController;